/*
 * process_handler.cpp
 *
 *  Created on: 2014. 4. 4.
 *      Author: wooyoung
 */

#include "process_handler.h"
#include "global_define.h"

ProcessHandler::ProcessHandler()
: BaseProcessHandler()
{

}

ProcessHandler::~ProcessHandler()
{

}

void ProcessHandler::DoWork()
{
	for (int i_for_loop = 0; i_for_loop < GlobalDefine::PROCESS_COUNT; i_for_loop++) {
		if (i_for_loop == 0) {//processing parent
			process_id_ = getpid();
			ManageProcess(i_for_loop);
			continue;
		}
		process_id_ = fork();

		if (process_id_ == -1) {//fork error

		} else if (process_id_ == 0) {//child process
			process_id_ = getpid();
			ManageProcess(i_for_loop);
			break;
		} else if (process_id_ > 0) {//parent process
		} else {//Should not get inside this line when it is normal.
			assert(0);
		}
	}
}

int ProcessHandler::ManageProcess(int process_index)
{
	switch (process_index) {
	case 0://Parent process starts
		printf("Hello Parent : %d\n", this->process_id_);
		break;
	case 1://File IO process starts
		printf("Hello File IO : %d\n", this->process_id_);
		break;
	case 2://Network IO process starts
		printf("Hello Network : %d\n", this->process_id_);
		break;
	default://Error Handling
		break;
	}
	return 0;
}
